#import "MailListViewController.h"
#import "IDominoAppDelegate.h"
#import "MailListCell.h"

@implementation  MailListViewController
@synthesize _tableView, toolbar, mailListProgressLabel, activityIndicator, currentMailList;

- (id)init {
	if (self = [super init]) {
		CGRect mainViewBounds = self.view.bounds;
		// Create a table viiew
		_tableView = [[UITableView alloc] initWithFrame:CGRectMake(CGRectGetMinX(mainViewBounds),
																   CGRectGetMinY(mainViewBounds) ,
																   CGRectGetWidth(mainViewBounds),
																   CGRectGetHeight(mainViewBounds)-50+4)
											style:UITableViewStylePlain];
		_tableView.autoresizingMask = (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight);
		_tableView.delegate = self;
		_tableView.dataSource = self;
		_tableView.rowHeight = 56.0;
		_tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
		_tableView.sectionHeaderHeight = 0;
		
		// Set the size and position for the "status" bar. This is actually a UIToolbar as we have clickable buttons on this

		toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(CGRectGetMinX(mainViewBounds),
															  CGRectGetMinY(mainViewBounds) + CGRectGetHeight(mainViewBounds) - 90,
															  CGRectGetWidth(mainViewBounds),
															  50)];
		


		self.hidesBottomBarWhenPushed=YES;
		self.title=NSLocalizedString(@"Inbox", @"Inbox title");
		
		IDominoAppDelegate *appDelegate = (IDominoAppDelegate *)[[UIApplication sharedApplication] delegate];
		
		UIBarButtonItem *composeItemContent = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:	UIBarButtonSystemItemCompose
																							target:appDelegate action:@selector(composeAction:)];
		self.navigationItem.rightBarButtonItem=composeItemContent;
		
		[composeItemContent release];
		
		mailListProgressLabel = [[UILabel alloc] initWithFrame:CGRectMake(40.0, 0.0, 180.0, 20)] ;
		mailListProgressLabel.autoresizingMask=YES;
		mailListProgressLabel.textAlignment = UITextAlignmentCenter;	
		mailListProgressLabel.font = [UIFont systemFontOfSize:14.0];
		mailListProgressLabel.backgroundColor = [UIColor clearColor];
		mailListProgressLabel.textColor=[UIColor whiteColor];
		mailListProgressLabel.adjustsFontSizeToFitWidth=YES;
		
		
		
		UIBarButtonItem *labelItem = [[UIBarButtonItem alloc] initWithCustomView:mailListProgressLabel];
		UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
																				  target:nil action:nil];
		UIBarButtonItem *refreshItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:	UIBarButtonSystemItemRefresh
																					 target:appDelegate action:@selector(refreshAction:)];
		
		activityIndicator=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
		UIBarButtonItem *activityItem = [[UIBarButtonItem alloc] initWithCustomView:activityIndicator];
		toolbar.items = [NSArray arrayWithObjects: activityItem, flexItem, labelItem, flexItem, refreshItem, nil];
		
		[flexItem release];
		[labelItem release];
		[refreshItem release];
		[activityItem release];
		[self.view addSubview:toolbar];
		[self.view addSubview:_tableView];

		currentMailList=[[NSArray alloc] init];

	}
	return self;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return  [currentMailList count]+1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{	
	
	IDominoAppDelegate *appDelegate = (IDominoAppDelegate *)[[UIApplication sharedApplication] delegate];
	if((indexPath.row==[currentMailList count]) ){
		if(indexPath.row != 0 && [appDelegate isDataSourceAvailable])
			[appDelegate readMoreMails]; 
		[tableView deselectRowAtIndexPath:indexPath animated:NO];
		return;
	}
	[appDelegate showMailInfo:[currentMailList objectAtIndex:indexPath.row]];
	[(MailListCell *)[tableView cellForRowAtIndexPath:indexPath] setCellRead];
	currentIndexPath=indexPath;
	[tableView deselectRowAtIndexPath:indexPath animated:NO];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	if(indexPath.row>=([currentMailList count])){
		UITableViewCell* cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MailListNAViewCell"] autorelease];
		if(indexPath.row==0){
			cell.textLabel.text=NSLocalizedString(@"N/A", @"Not Available");
			cell.selectionStyle=UITableViewCellSelectionStyleNone;
		}
		else{
			IDominoAppDelegate *appDelegate = (IDominoAppDelegate *)[[UIApplication sharedApplication] delegate];
			if([currentMailList count]==[appDelegate totalMailCount])
				cell.textLabel.text=[NSString stringWithFormat:@"%d%@", [currentMailList count], NSLocalizedString(@"TotalLoad"," mail loaded in all.")];
			else
				cell.textLabel.text = [NSString stringWithFormat:@"%d/%d %@",[currentMailList count], 
						 [appDelegate totalMailCount],NSLocalizedString(@"LoadMore", @"already loaded, More...") ];
		}
		cell.textLabel.textColor = [UIColor colorWithWhite:0.5 alpha:0.5];
		cell.accessoryType=UITableViewCellAccessoryNone;
		return cell;
	}
	static NSString *MyIdentifier = @"MailListViewCell";
    
  	MailListCell *cell = (MailListCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
	if (cell == nil) {
		cell = [[[MailListCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
	}

	Mail *mailForRow = [currentMailList objectAtIndex:indexPath.row];
	[cell setMail:mailForRow];
	cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
	return cell;
}



- (void)dealloc {
	[_tableView release];
	[activityIndicator release];
	[toolbar release];
	[currentMailList release];
	[super dealloc];
}

-(void) setDisplayList:(NSArray*)list
{
	currentMailList=[list retain];
	[_tableView reloadData];
}



-(void)setMailStatusBarWithMessage:(NSString*)message animateIndicator:(BOOL)needAnimated
{
	[mailListProgressLabel setText:message];
	if(needAnimated==YES)
		[activityIndicator startAnimating];
	else
		[activityIndicator stopAnimating];
	[self.toolbar setNeedsDisplay];
}

- (NSIndexPath*)currentIndexPath
{
	return currentIndexPath;
}
@end
